package MineGod;

import java.util.Random;

import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.opengl.Texture;

public class EntityMobBat extends EntityMob {
	private final double viewDist = 10*Block.WIDTH;
	private boolean hasSeenPlayer = false;
	
	public static Texture batTex;
	
	public EntityMobBat(double xPos, double yPos){
		maxSpeed = .1;
		x = xPos;
		y = yPos;
		height = 12;
		width = 12;
		maskWidth = 8;
		maskHeight = 8;
		hP = 100;
		maxHP = 100;
		mask = new Rectangle((float)(x+(getWidth()-maskWidth)/2), (float)(y+(getHeight()-maskHeight)/2), (float)maskWidth, (float)maskHeight);
		zIndex = -2;
	}
	
	@Override
	public Texture getTexture() {
		// TODO Auto-generated method stub
		return batTex;
	}

	@Override
	public double getWidth() {
		// TODO Auto-generated method stub
		return width;
	}

	@Override
	public double getHeight() {
		// TODO Auto-generated method stub
		return height;
	}

	@Override
	public void interactWithPlayerOnCollision(Player player) {
		// TODO Auto-generated method stub
		//System.out.println("Wombo Combo!!!!11!!11!1!");
		player.damage(10, DamageType.mobHit);
	}

	@Override
	public void doLogic(int delta, Chunk prevChunk, Chunk currChunk,
			Chunk nextChunk) {
		//find player.
		
		ySpeed = 0;
		xSpeed = 0;
		double slowdown = 1;
		Block b = Utils.pointToBlock(x+getWidth()/2, y+getHeight()/2);
		if (!(b==null) && b.isLiquid){
			slowdown = .6;
		}else if (!(b==null) && b.isSolid){
			slowdown = 0;
		}
		double posSpeed = maxSpeed * slowdown;
		Player p = Utils.getPlayer();
		//If player is in view distance
		if (hasSeenPlayer || (Math.sqrt(Math.pow(x-p.xPos,2)+Math.pow(y-p.yPos,2))<=viewDist && true /*player is visable*/)){
			double xDiff = x-p.xPos;
			double yDiff = y-p.yPos;
			double angle;
			if (xDiff > 0){
				angle = Math.atan(yDiff/xDiff);
			} else {
				angle = Math.atan(yDiff/xDiff) + Math.PI;
			}
			xSpeed = -Math.cos(angle)*maxSpeed*slowdown;
			ySpeed = -Math.sin(angle)*maxSpeed*slowdown;
			
		}else{
			Random rand = Chunk.rand;
			
			if (rand.nextBoolean()){
				xSpeed = posSpeed;
			}else{
				xSpeed = -posSpeed;
			}
			if (rand.nextBoolean()){
				ySpeed = posSpeed;
			}else{
				ySpeed = -posSpeed;
			}
		}
		
		//Now check physics stuff
		horizontalPhysics(delta,prevChunk,currChunk,nextChunk);
		climbingPhysics(delta,prevChunk,currChunk,nextChunk);
		x += xSpeed*delta;
		y += ySpeed*delta;

		mask.setLocation((float)(x + (maskWidth-width)/2), (float)(y+(maskHeight-height)/2));

		
		updateOnFire(delta);
		updateInvincibility(delta);
		
		Block b2 = Utils.pointToBlock(x+width/2, y+width/2);
		if (!(b2==null) && b2.type == BlockType.lava){
			this.damage(15, DamageType.lava);
		}
		
	}

}
